/**
 * 测试用执行接口测试用例
 *
 * Created by snowingsea on 2020/03/15.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const sd = require('./../../../benchmark/run-tests/self_designed.json');
const benchmarks = [sd];

// const files = require('./../../../../phoenix/models/files');

module.exports = describe('/POST project-versions/:id/results.json', function () {
  const administrator = new Administrator();
  let project = {};
  const newUserIds = [];
  function url(versionId) {
    return `/project-versions/${versionId}/results.json`;
  }

  // /**
  //  * 补全函数信息
  //  * @param {Object} tree 树结构
  //  * @return {Promise}
  //  */
  // function completeFunctionLeaf(tree) {
  //   if (tree.kind === files.kind.directory) {
  //     const promises = tree.children.map((child) => completeFunctionLeaf(child));
  //     return Promise.all(promises);
  //   }
  //   return new Promise((resolve, reject) => {
  //     chai.request(uri)
  //       .get(`/files/${tree.fileId}/functions.json`)
  //       .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
  //       .end((err, res) => {
  //         if (err) {
  //           reject(err);
  //           return;
  //         }
  //         if (res.body.functions.length !== 0) {
  //           tree.children = res.body.functions;
  //         }
  //         resolve();
  //       });
  //   });
  // }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'post');
  common.should.be.tester(uri, url, 'post');

  // 正常发起请求
  benchmarks.forEach((benchmark) => {
    const extensions = benchmark.config.languages.c.suffixes
      .concat(benchmark.config.headers.suffixes);
    it(`it should AUTO TEST of ${benchmark.name}`, function (done) {
      let projectVersion;
      project.createRandomVersion()
        .then((res) => {
          projectVersion = res;
          return projectVersion.updateVersionFiles(benchmark.files, benchmark.config);
        })
        .then(() => project.creator.login())
        .then(() => {
          chai.request(uri)
            .post(url(projectVersion.versionId))
            .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
            .end((err, res) => {
              res.should.have.status(202);
              common.should.be.taskMessage(res.body);

              let actualTree;
              project.creator.waiting4TasksOfVersionFinished(projectVersion.versionId)
                .then(() => projectVersion.rootDirectory.getTreeAll(extensions))
                .then((res) => {
                  actualTree = res;
                  common.should.be.fileTreeAllMessage(actualTree);
                  common.should.be.equal.tree(actualTree.tree, benchmark.result.tree);
                  done();
                })
                .catch(done);
            });
        })
        .catch(done);
    });
  });
});
